package com.xianye.erp.service.order.impl;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.annotation.Resource;

import com.xianye.erp.dao.base.DaoSupport;
import com.xianye.erp.util.PageData;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;


@Service("refreshOrderService")
public class RefreshOrderService {
	protected Logger log = Logger.getLogger(this.getClass());
	private final static Executor executor = Executors.newCachedThreadPool();//启用多线程
	@Autowired
	private JdbcTemplate jdbc;
	@Autowired
	private ExpressOrderAPI expressOrderAPI;
	@Resource(name = "daoSupport")
	private DaoSupport dao;
	@Autowired
	private OrderAPI orderAPI;
	
	public void refresh(final String createDateStart,final String createDateEnd,final String status){
		try {
			List<PageData> list = getStore();
			System.out.println(list.size());
			for(int i=0;i<list.size();i++){
				PageData store = list.get(i);
				refreshOneStore(store,createDateStart,createDateEnd,status);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void refreshOne(final String storeId, final String createDateStart,final String createDateEnd,final String status){
		try {
			    PageData pd =new PageData();
			    pd.put("storeId", storeId);
				PageData store = getOneStore(pd);
				refreshOneStore(store,createDateStart,createDateEnd,status);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public PageData getOneStore(PageData pd) throws Exception{
		return (PageData) dao.findForObject("StoreMapper.findByStoreId", pd);
	}
	public List<PageData> getStore() throws Exception{
		return (List<PageData>) dao.findForList("StoreMapper.list", new PageData());
	}
	
	public void refreshOneStore(PageData store, String createDateStart,String createDateEnd,String status){
		try {
			refreshOneStoreByStatusAndDate(store, createDateStart, createDateEnd, status);
			if(status==null){
				refreshOneStoreByStatusAndDate(store, createDateStart, createDateEnd, "FINISH");

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	public void refreshOneStoreByStatusAndDate(PageData store, String createDateStart,String createDateEnd,String orderStatus) throws Exception{
		try {
			JSONArray array = new JSONArray();
			JSONObject json  = expressOrderAPI.getOrderList(store,1L, 1L,createDateStart,createDateEnd,orderStatus);
			Integer total = 0;
			if(json!=null && json.containsKey("total_count")){
				total = json.getInt("total_count");
			}
			log.error("数据总数"+total);
			int page = (int) Math.ceil(total/50.0);
			for(int i=1;i<=page;i++){
				JSONObject obj = expressOrderAPI.getOrderList(store, (long)i,50L,createDateStart,createDateEnd,orderStatus);
				if(obj!=null&& obj.containsKey("target_list")){
					array.addAll(obj.getJSONObject("target_list").getJSONArray("aeop_order_item_dto"));
				}
			}
			//开始调用处理数据逻辑
			JSONArray temp = new JSONArray();
			for(int i=0;i<array.size();i++){				
				temp.add(array.get(i));
				if(i%50==0){
					orderAPI.operate(store, temp);
					temp.clear();
				}				
			}
			if(temp.size()>0){
				orderAPI.operate(store, temp);
				temp.clear();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("数据获取抛出异常信息：",e);
		}
		log.info("结束同步店铺订单,开始时间："+createDateStart +"结束时间："+ createDateEnd+ "店铺ID："+store.get("ID"));
	}
	

}
